package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.heima.common.dto.ResponseResult;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author SpaceTraveller
 * @since 2022-11-07
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {

    @Override
    public ResponseResult<ApAuthor> saveAuthor(ApAuthor author) {
        // 查询用户是否已存在作者数据
        LambdaQueryWrapper<ApAuthor> query = new LambdaQueryWrapper<ApAuthor>().eq(ApAuthor::getUserId, author.getUserId());
        ApAuthor old = this.getOne(query);
        // 如果存在数据了,那么直接返回
        if (old != null) {
            return ResponseResult.okResult(old);
        }
        // 没有,那么补全信息并保存数据
        author.setCreatedTime(new Date());
        this.save(author);
        return ResponseResult.okResult(author);
    }
}
